package com.sc.nft.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.ReportBalance;
import com.sc.nft.entity.vo.*;
import com.sc.nft.helper.LlPayHelper;
import com.sc.nft.service.ReportAmountService;
import com.sc.nft.service.ReportBalanceService;
import com.sc.nft.service.ReportTransferService;
import com.sc.nft.service.ReportUserService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/report")
@Api(tags = "后台报表")
public class AdminReportController {

    private final ReportBalanceService reportBalanceService;
    private final ReportAmountService reportAmountService;
    private final ReportTransferService reportTransferService;
    private final ReportUserService reportUserService;
    private final LlPayHelper llPayHelper;

    @PostMapping(value = "/getReportBalance", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取平台数据概况", notes = "获取平台数据概况", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminReportBalanceVO> getReportBalance() {
        AdminReportBalanceVO adminReportBalanceVO = reportBalanceService.getLastOneDay();
        adminReportBalanceVO.setCreateTime(DateUtil.offsetDay(adminReportBalanceVO.getCreateTime(),-1));
        return Result.success(adminReportBalanceVO);
    }

    @PostMapping(value = "/getPageReportAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取财务数据统计", notes = "获取财务数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ReportAmountVO> getPageReportAmount(@Valid @RequestBody PageReportAmountRequest request) {
        return PageResult.success(reportAmountService.getPageReportAmount(request.getBeginTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/getTotalReportAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取财务数据合计", notes = "获取财务数据合计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<TotalReportAmountVO> getTotalReportAmount(@Valid @RequestBody TotalReportAmountRequest request) {
        return Result.success(reportAmountService.getTotalReportAmount(request.getBeginTime(),request.getEndTime()));
    }

    @PostMapping(value = "/getReportTransfer", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取交易数据统计", notes = "交易数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ReportTransferVO> getReportTransfer() {
        ReportTransferVO reportTransfer = reportTransferService.getReportTransfer();
        reportTransfer.setCreateTime(DateUtil.offsetDay(reportTransfer.getCreateTime(),-1));
        return Result.success(reportTransfer);
    }

    @PostMapping(value = "/getPageReportTransfer", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取分页交易数据统计", notes = "获取分页交易数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PageReportTransferVO> getPageReportTransfer(@Valid @RequestBody PageReportTransferRequest request) {
        return PageResult.success(reportTransferService.getPageReportTransfer(request.getType(),request.getBeginTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }


    @PostMapping(value = "/getReporUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户数据统计", notes = "获取用户数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ReportUserVO> getReporUser() {
        return Result.success(reportUserService.getReporUser());
    }

    @PostMapping(value = "/getPageReporUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取分页用户数据统计", notes = "获取分页用户数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ReportUserDayDetailsVO> getPageReporUser(@Valid @RequestBody PageReportAmountRequest request) {
        return PageResult.success(reportUserService.getPageReporUser(request.getBeginTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/queryLinked", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取分页用户数据统计", notes = "获取分页用户数据统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject queryLinked(@Valid @RequestBody IdAndOrderNoRequest request) {
        return llPayHelper.queryLinked(request.getOriOrderNo());
    }

    @PostMapping(value = "/transferTmp", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "临时退回给公司", notes = "临时退回给公司", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject transferTmp(@Valid @RequestBody LoginRequest request) {
        return llPayHelper.transferTmp(request.getAccount(),request.getPassword());
    }
}
